package libs.yzx.zaza.zaza2;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES 加密实现类   by : YZX
 */
public class AESUtil {

    public static String encrypt(String data , String key) {
        try {
            SecretKeySpec spec = new SecretKeySpec(key.getBytes("UTF-8"),"AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE , spec,new IvParameterSpec(new byte[cipher.getBlockSize()]));
            byte[] bs = cipher.doFinal(data.getBytes("UTF-8"));
            return Base64.encode(bs);
        } catch (Exception e) {
            return null;
        }
    }


    public static String decrypt(String data, String key) {
        try {
            SecretKeySpec spec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE , spec , new IvParameterSpec(new byte[cipher.getBlockSize()]));
            byte[] originBytes = Base64.decode(data);
            byte[] result = cipher.doFinal(originBytes);
            return new String(result,"UTF-8");
        } catch (Exception e) {
            return null;
        }
    }




//    public static String encrypt(String data , String key) {
//        try {
//            KeyGenerator kgen =  KeyGenerator.getInstance("AES",new BouncyCastleProvider());
//            kgen.init(128 , new SecureRandom(key.getBytes("UTF-8")));
//            byte[] keyEncode = kgen.generateKey().getEncoded();
//            SecretKeySpec spec = new SecretKeySpec(keyEncode, "AES");
//            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//            cipher.init(Cipher.ENCRYPT_MODE , spec,new IvParameterSpec(new byte[cipher.getBlockSize()]));
//            byte[] bs = cipher.doFinal(data.getBytes("UTF-8"));
//            return Base64.encodeToString(bs,Base64.DEFAULT);
//        } catch (Exception e) {
//            return null;
//        }
//    }
//
//
//    public static String decrypt(String data, String key) {
//        try {
//            KeyGenerator kgen =  KeyGenerator.getInstance("AES",new BouncyCastleProvider());
//            kgen.init(128 , new SecureRandom(key.getBytes("UTF-8")));
//            byte[] keyEncode = kgen.generateKey().getEncoded();
//            SecretKeySpec spec = new SecretKeySpec(keyEncode, "AES");
//            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//            cipher.init(Cipher.DECRYPT_MODE , spec , new IvParameterSpec(new byte[cipher.getBlockSize()]));
//            byte[] originBytes = Base64.decode(data, Base64.DEFAULT);
//            byte[] result = cipher.doFinal(originBytes);
//            return new String(result,"UTF-8");
//        } catch (Exception e) {
//            return null;
//        }
//    }
//




//
//    /****************************************************************************************************************
//     * @param key  key
//     * @param data 数据
//     * @return 密文
//     */
//    public static String encrypt(String key, String data) {
//        byte[] arrayOfByte = null;
//        try {
//            arrayOfByte = encrypt(getRawKey(key.getBytes()), data.getBytes());
//        } catch (Exception localException) {
//            localException.printStackTrace();
//        }
//        if (arrayOfByte != null)
//            return toHex(arrayOfByte);
//        return null;
//    }
//
//
//    /****************************************************************************************************************
//     * @param key  key
//     * @param data 密文
//     * @return 明文
//     */
//    public static String decrypt(String key, String data) {
//        try {
//            byte[] rawKey = getRawKey(key.getBytes());
//            byte[] dataByte = toByte(data);
//            return new String(decrypt(rawKey, dataByte));
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//
//
//    //
//    //
//
//
//    @SuppressLint("TrulyRandom")
//    private static byte[] getRawKey(byte[] paramArrayOfByte) throws Exception {
//        KeyGenerator localKeyGenerator = KeyGenerator.getInstance("AES");
//        SecureRandom localSecureRandom;
//        (localSecureRandom = SecureRandom.getInstance("SHA1PRNG")).setSeed(paramArrayOfByte);
//        localKeyGenerator.init(128, localSecureRandom);
//        return localKeyGenerator.generateKey().getEncoded();
//    }
//
//    private static byte[] encrypt(byte[] keyByte, byte[] dataByte) throws Exception {
//        SecretKeySpec sks = new SecretKeySpec(keyByte, "AES");
//        Cipher localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//        localCipher.init(1, sks, new IvParameterSpec(new byte[localCipher.getBlockSize()]));
//        return localCipher.doFinal(dataByte);
//    }
//
//    private static byte[] decrypt(byte[] keyByte, byte[] dataByte) throws Exception {
//        SecretKeySpec sks = new SecretKeySpec(keyByte, "AES");
//        Cipher localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//        localCipher.init(2, sks, new IvParameterSpec(new byte[localCipher.getBlockSize()]));
//        return localCipher.doFinal(dataByte);
//    }
//
//    private static byte[] toByte(String paramString) {
//        int i;
//        byte[] arrayOfByte = new byte[i = paramString.length() / 2];
//        for (int j = 0; j < i; j++)
//            arrayOfByte[j] = Integer.valueOf(paramString.substring(2 * j, 2 * j + 2), 16).byteValue();
//        return arrayOfByte;
//    }
//
//    private static String toHex(byte[] paramArrayOfByte) {
//        if (paramArrayOfByte == null)
//            return "";
//        StringBuffer localStringBuffer = new StringBuffer(2 * paramArrayOfByte.length);
//        for (int i = 0; i < paramArrayOfByte.length; i++)
//            appendHex(localStringBuffer, paramArrayOfByte[i]);
//        return localStringBuffer.toString();
//    }
//
//    private static void appendHex(StringBuffer paramStringBuffer, byte paramByte) {
//        paramStringBuffer.append("0123456789ABCDEF".charAt(paramByte >> 4 & 0xF)).append("0123456789ABCDEF".charAt(paramByte & 0xF));
//    }

}